package com.cuit.qx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuit.qx.aop.OperLog;
import com.cuit.qx.entity.CorrectForecast;
import com.cuit.qx.entity.Product;
import com.cuit.qx.job.TimingProcessorJob;
import com.cuit.qx.service.CorrectForecastService;
import com.cuit.qx.service.ProductService;
import com.cuit.qx.utils.ReturnData;
import com.cuit.qx.utils.ReturnStateCode;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订正预报
 * </p>
 *
 * @author lcj
 * @since 2021-11-23
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/correctForecast")
public class CorrectForecastController {

    @Autowired
    private CorrectForecastService service;

    @Resource
    private ProductService productService;

    private static final Logger LOG = LoggerFactory.getLogger(CorrectForecastController.class);
    /**
     * @description: 条件筛选订正预报数据  pattern:模式，startTime:起报,时效 moment， 数据时间：date
     * 实例：http://127.0.0.1:8088/correctForecast/getForecastData?pattern=0&startTime=20&date=2020-10-26&moment=60&
     * countyName=苍溪
     * @return:
     * @author: wjd
     * @time: 2021/11/23 14:40
     */
    @RequestMapping("/getForecastData")
    public ReturnData conditionGetForecastData( HttpServletRequest req){
        String pattern = req.getParameter("pattern");
        String startTime = req.getParameter("startTime");
        String date = req.getParameter("date");
        String countyName = req.getParameter("countyName");
        String s = String.valueOf(pattern);
        if ("null" .equals(s)){
            return new ReturnData(ReturnStateCode.FAILEDCODE.getCode(),ReturnStateCode.FAILEDCODE.getMsg());
        }
        if (null == startTime || "".equals(startTime)){
            return new ReturnData(ReturnStateCode.FAILEDCODE.getCode(),ReturnStateCode.FAILEDCODE.getMsg());
        }

        if (null == date || "".equals(date)){
            return new ReturnData(ReturnStateCode.FAILEDCODE.getCode(),ReturnStateCode.FAILEDCODE.getMsg());
        }

        LambdaQueryWrapper<CorrectForecast> wrapper =new LambdaQueryWrapper();

        wrapper.eq(CorrectForecast::getPattern,pattern);

        wrapper.eq(CorrectForecast::getStartTime,startTime);

        wrapper.eq(CorrectForecast::getStartDate,date);

        if (countyName!=null)wrapper.eq(CorrectForecast::getRemark,countyName);

        List<CorrectForecast> list = service.list(wrapper);

        //数据查询成功后进行数据过滤

        return new ReturnData(ReturnStateCode.SUCCEE.getCode(),ReturnStateCode.SUCCEE.getMsg(),list);
    }
    /**
     * @description: 定时任务采集订正预报数据
     * @return:
     * @author: wjd
     * @time: 2021/11/23 14:41
     */
    public void gatherForecast(){

    }

    /**
     * @description: 定时删除一个月的订正预报数据数据
     * @return:
     * @author: wjd
     * @time: 2021/11/23 17:03
     */
    @RequestMapping("/deleteForecastData")
    public void removeForecastByDate(){
        service.removeForecastByDate();
    }

    @PostMapping("/makeProduce")
    @OperLog(operModul = "产品制作", operType = "1", operDesc = "订正图文产品制作")
    public ReturnData makeProduct(@RequestBody Map<String, Object> map) {
        try {
            String pattern = map.get("pattern").toString();
            String date = map.get("date").toString();
            String startTime = map.get("startTime").toString();
            String moment = map.get("moment").toString();
            String stationName = (String) map.get("stationName");

            String forecastUsr = map.get("forecastUsr").toString();
            String enteringUsr = map.get("enteringUsr").toString();
            String verifyUsr = map.get("verifyUsr").toString();
            String department = map.get("department").toString();
            Product product = new Product().setForecastUsr(forecastUsr)
                    .setEnteringUsr(enteringUsr)
                    .setVerifyUsr(verifyUsr)
                    .setDepartment(department)
                    .setStartDate(date)
                    .setStartTime(startTime)
                    .setProductMoment(Integer.valueOf(moment))
                    .setPattern(pattern);
            return  service.makeProduct(pattern, date, startTime, moment, stationName,product);
        } catch (NullPointerException ex){
            return new ReturnData(ReturnStateCode.FAILEDCODE);
        }catch (Exception e) {
            return new ReturnData(ReturnStateCode.FAILED,e.getMessage());
        }

    }

    @RequestMapping("/getByNoAndStaion")
    public ReturnData getByNoAndStaion(String startDate,String startTime,String staionId,String id){

        LambdaQueryWrapper<CorrectForecast> wrapper =new LambdaQueryWrapper();

        if (null!=startDate) wrapper.eq(CorrectForecast::getStartDate,startDate);
        if (null!=startTime) wrapper.eq(CorrectForecast::getStartTime,startTime);
        if (null!=staionId) wrapper.eq(CorrectForecast::getStationId,staionId);
        if (null!=id) wrapper.eq(CorrectForecast::getId,id);

        List<CorrectForecast> list = service.list(wrapper);
        if (list.size()>0){
            return  new ReturnData(ReturnStateCode.SUCCEE.getCode(),ReturnStateCode.SUCCEE.getMsg(),list);
        }
        return  new ReturnData(ReturnStateCode.SUCCEEBUT.getCode(),ReturnStateCode.SUCCEEBUT.getMsg());


    }
    /**
     * @description: 根据id修改订正预报值
     * @return:
     * @author: wjd
     * @time: 2021/12/27 11:38
     */
    @RequestMapping(value ="/byListIdApudate",method = RequestMethod.POST)
    public ReturnData byListIdApudate(@RequestBody List<CorrectForecast> correctForecasts){
        correctForecasts.stream().forEach(
                item->{
                    service.updateById(item);
                }
        );
        return new ReturnData(ReturnStateCode.SUCCEE.getCode(),ReturnStateCode.SUCCEE.getMsg());
    }
}
